<?php
	class AdminDthink_Model_Category extends Zendvn_Model_Nested
	{
		protected $_name = 'category';
		protected $_primary = 'cat_id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
		
		public function listCategory()
		{
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->order('lft ASC');
			$query = $this->_db->fetchAll($sql);
			return $query;
		}
        
        public function getCatNameById($cat_id)
        {
            $sql = $this->_db->select('cat_name')
                            ->from($this->_name)
                            ->where('cat_id = ?', $cat_id);
            $result = $this->_db->fetchRow($sql);
            return $result['cat_name'];
        }
		
		public function listVoucher($parents)
		{
			$sql = $this->_db->select()
							->from($this->_name)
							->where('parents = ?', $parents)
                            ->where('status = 1')
							->order('lft ASC');
			$result = $this->_db->fetchAll($sql);
			return $result;
		}
		
		public function checkCategory($cat_name)
		{
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where('cat_name = ?', $cat_name);
			$result = $this->_db->fetchAll($sql);
			return count($result);
		}
		
		public function searchCategory($keyword)
		{
			$where = "MATCH(cat_name) AGAINST('" . $keyword . "' IN NATURAL LANGUAGE MODE)";
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where($where);
			$result = $this->_db->fetchAll($sql);
			return $result;
		}
        
        public function listMenu()
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('level = 1')
                            ->order('lft ASC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
	}